package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zp
 * @program tensuqare
 * @description
 * @create 2019-05-19 18:33
 **/
@Service
public class LabelService {
    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部标签
     *
     * @return
     */
    public List<Label> findAll() {
        return labelDao.findAll();
    }

    /**
     * 根据ID查询标签
     *
     * @return
     */
    public Label findById(String id) {
        return labelDao.findById(id).get();
    }

    /**
     * 增加标签
     *
     * @param label
     */
    public void add(Label label) {
        label.setId(idWorker.nextId() + "");//设置ID
        labelDao.save(label);
    }

    /**
     * 修改标签
     *
     * @param label
     */
    public void update(Label label) {
        labelDao.save(label);
    }

    /**
     * 删除标签
     *
     * @param id
     */
    public void deleteById(String id) {
        labelDao.deleteById(id);
    }

    public List<Label> findSearch(Label label) {

        Specification<Label> specification = new Specification<Label>() {
            List<Predicate> list = new ArrayList<>();
            /**
             *
             * @param root 根对象 where 类名=label.getId
             * @param criteriaQuery 查询条件 关键字 group by /order by
             * @param criteriaBuilder 用来封装对象的
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                if (!StringUtils.isEmpty(label.getLabelname())) {
                    Predicate predicate = criteriaBuilder.like
                            (root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    list.add(predicate);
                }
                if (!StringUtils.isEmpty(label.getState())) {
                    Predicate predicate = criteriaBuilder.like
                            (root.get("state").as(String.class), "%" + label.getState() + "%");
                    list.add(predicate);
                }
                Predicate[] parr = new Predicate[list.size()];
                parr = list.toArray(parr);
                return criteriaBuilder.and(parr);
            }
        };
        List<Label> list = labelDao.findAll(specification);
        return list;
    }

    public Page<Label>  pageQuery(Label label, int page, int size) {
        Pageable pageable;
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        Specification<Label> specification = new Specification<Label>() {
            List<Predicate> list = new ArrayList<>();
            /**
             *
             * @param root 根对象 where 类名=label.getId
             * @param criteriaQuery 查询条件 关键字 group by /order by
             * @param criteriaBuilder 用来封装对象的
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                if (!StringUtils.isEmpty(label.getLabelname())) {
                    Predicate predicate = criteriaBuilder.like
                            (root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    list.add(predicate);
                }
                if (!StringUtils.isEmpty(label.getState())) {
                    Predicate predicate = criteriaBuilder.like
                            (root.get("state").as(String.class), "%" + label.getState() + "%");
                    list.add(predicate);
                }
                Predicate[] parr = new Predicate[list.size()];
                parr = list.toArray(parr);
                return criteriaBuilder.and(parr);
            }
        };
        return labelDao.findAll(specification, pageRequest);

    }
}
